<?php 
defined('BASEPATH') OR exit('No direct script access allowed');
class House extends MY_Controller 
{
	public function __construct()
	{
		parent::__construct();
		$this ->load->helper('url');
		$this ->load->database();
	}
	/**
	 * 房屋信息列表
	 */
	public function index($offset='')
	{
		$this->load->model('House_model');
		$perPage =2;

		//分页
		$this->load->library('pagination');
		//$config['base_url'] = "http://127.0.0.1/study/htdocs/php5/ci/index.php/house/index";
		$config['base_url'] =  site_url('admin/house/index');;
		$config['total_rows'] = $this->db->count_all_results('house');

		$config['per_page'] =$perPage;

		$config['first_link'] = '第一页';
		$config['prev_link'] = '上一页';
		$config['next_link'] = '下一页';
		$config['last_link'] = '最后一页';

		$this->pagination->initialize($config);
		$arr['links'] = $this->pagination->create_links();

		$limit = $config['per_page'];

		//$aa = $this->db->limit($perPage,$offset);

		$arr['house'] = $this->House_model->chak($limit,$offset);
		// print_r($arr);exit();
/*		$this->load->view('home/commont/head');
		$this->load->view('home/commont/left');*/
		$this->load->view('admin/house/index',$arr);
	}
	/**
	 * 房屋信息添加
	 */
	public function add()
	{
		//房屋类型分类
		$this->load->model('Type_model');
		$arr['type'] = $this->Type_model->chak();
		//楼盘编号
		$this->load->model('Floor_model');
		$arr['floor'] = $this->Floor_model->huset_check();
		//添加操作
		$method = !empty($_GET) ? 'GET' : 'POST';
		//上面只是用来判断用什么方式提交的，顺便还可以映射一下,使用的话就更方便了
		if(!empty($_POST)){
			$config['upload_path'] = './uploads/';
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size'] = '10000';
			$config['file_name'] = time().mt_rand(1000,9999);
			//载入上传类
			$this->load->library('upload', $config);
			//执行上传
			$status = $this->upload->do_upload('house_img');
			if(!$status)
			{
				echo '上传失败';die();
			}
			//返回信息
			$info = $this->upload->data();
			$data = array(
				'floor_sn'=>$this->input->post('floor_sn'),
				'house_sn'=>'house_'.time(),
				'F'=>$this->input->post('F'),
				'house_img'=>$info['file_name'],
				'house_price'=>$this->input->post('house_price'),
				'house_type'=>$this->input->post('house_type'),
				'house_cate'=>$this->input->post('house_cate'),
				'house_s'=>$this->input->post('house_s'),
				'use_s'=>$this->input->post('use_s'),
				
				);
			 $this->load->model('House_model');
			$arr = $this->House_model->add($data);
			if(!$arr)
			{
				echo "添加失败";die();
			}
			redirect('admin/house/index');

		}
		// print_r($arr);die;
		$this->load->view('admin/house/add',$arr);
	}
	/**
	 * 房屋信息修改
	 */
	public function update()
	{
		//分类查询
		$this->load->model('Type_model');
		$arr['type'] = $this->Type_model->chak();
			//楼盘编号
		$this->load->model('Floor_model');
		$arr['floor'] = $this->Floor_model->huset_check();
		//根据修改传过来的id查询
		$id=$this->input->get('id');
		$this->load->model('House_model');
		$arr['house'] = $this->House_model->updas($id);
		// print_r($arr['house']);exit();
		//接受修改传过来的值
		$method = !empty($_GET) ? 'GET' : 'POST';
		//上面只是用来判断用什么方式提交的，顺便还可以映射一下,使用的话就更方便了
		if(!empty($_POST)){
			//接受修改传过来的id
			$id = $this->input->post('house_id');
			// print_r($id);exit();
			$config['upload_path'] = './uploads/';
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size'] = '10000';
			$config['file_name'] = time().mt_rand(1000,9999);
			//载入上传类
			$this->load->library('upload', $config);
			//执行上传
			$status = $this->upload->do_upload('house_img');
			// if(!$status)
			// {
			// 	echo '上传失败';die();
			// }
			//返回信息
			$info = $this->upload->data();
			//判断是否修改了图片
			$img = $info['file_name'];
			if(strlen($img)<15){
				$this->load->model('House_model');
				$arr = $this->House_model->updas($id);
				$imgs = $arr['house_img'];
			}else
			{
				$imgs = $info['file_name'];
			}
				$data = array(
						'floor_sn'=>$this->input->post('floor_sn'),
						'house_sn'=>$this->input->post('house_sn'),
						'F'=>$this->input->post('F'),
						'house_img'=>$imgs,
						'house_price'=>$this->input->post('house_price'),
						'house_type'=>$this->input->post('house_type'),
					);
			$this->load->model('House_model');
			$arr = $this->House_model->update($id,$data);
			if(!$arr)
			{
				echo "修改失败";die();
			}
			redirect('admin/house/index');

		}
/*		$this->load->view('home/commont/head');
		$this->load->view('home/commont/left');*/
		$this->load->view('admin/house/update',$arr);
	}
	/**
	 * 删除
	 */
	public function del()
	{
		$id=$this->input->get('id');
		$this->load->model('House_model');
		$arr = $this->House_model->del($id);
		if(!$arr)
			{
				echo "删除失败";die();
			}
			redirect('admin/house/index');
	}
}